package com.xiaoxu.spzx.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaoxu.spzx.manager.service.SysUserService;
import com.xiaoxu.spzx.model.dto.system.AssignRoleDto;
import com.xiaoxu.spzx.model.dto.system.SysUserDto;
import com.xiaoxu.spzx.model.entity.system.SysUser;
import com.xiaoxu.spzx.model.vo.common.Result;
import com.xiaoxu.spzx.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    //分配角色 -> 2将用户id,角色id(List)添加到sys_user_role中

    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssignRoleDto assignRoleDto) {
        sysUserService.doAssign(assignRoleDto) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }


    //1 用户分页条件查询的
    //pageNum  -- current 当前页
    //pageSize  -- limit 每页记录数

    @GetMapping ("/findByPage/{pageNum}/{pageSize}")
    public Result SysUserListByPage(@PathVariable("pageNum") Integer pageNum,
                                    @PathVariable("pageSize")  Integer pageSize,
                                     SysUserDto sysUserDto){
     PageInfo<SysUser> pageInfo =  sysUserService.selectByPage(pageNum,pageSize,sysUserDto);
     return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }


    //2 用户添加
    @PostMapping("/saveSysUser")
    public Result saveSysUser(@RequestBody SysUser sysUser){

        sysUserService.saveSysUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    //3 用户修改
    @PutMapping("/updateSysUser")
    public Result updateSysUser(@RequestBody SysUser sysUser){

        sysUserService.updateSysUser(sysUser);

        return Result.build(null, ResultCodeEnum.SUCCESS);

    }



    //4 用户逻辑删除
    @DeleteMapping("/deleteById/{userId}")
    public Result deleteSysUser(@PathVariable("userId") Long userId){

        sysUserService.deleteById(userId);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }



}
